﻿#ifndef MC_CONFIG_H
#define MC_CONFIG_H
#define CONFIG_TEMP_PROT_NUM 2
#include "types.h"
typedef enum {
    PID_ID_ID,
    PID_IQ_ID,
    PID_Vel_ID,
    PID_VelLim_ID,
    PID_IDCLim_ID,
    PID_AutoHold_ID,
    PID_EPM_ExtID,
    PID_Max_ID,
}PID_id_t;

typedef struct
{
    u8 poles;
    float ld;
    float lq;
    float rs;
    float flux;
    float J;
    s16   max_rpm;
    u16	  max_fw_id;
    bool  fw_enable;
    float fw_duty_start; //0-100
    s16   enc_pll_band;
    float encoder_offset;
}motor_params_t;

typedef struct {
    s16 vbus_voltage_max;    //if higher than max, motor will force stop and can not start
    s16 vbus_voltage_min;    //if lower than min, motor will force stop and can not start
    s16 vbus_curr_max;
    s16 phase_curr_max;      //max phase current
    float max_regen_current; //max regenerator current
    float brake_resistance;  //the brake resistance to sink the power which is regenerated by the motor
    bool enable_brake_resistor;
    /**
     * If enabled, if the measured DC voltage exceeds `dc_bus_overvoltage_ramp_start`,
     * the Controller will sink more power than usual into the the brake resistor
     * in an attempt to bring the voltage down again.
     *
     * The brake duty cycle is increased by the following amount:
     *  vbus_voltage == dc_bus_overvoltage_ramp_start  =>  brake_duty_cycle += 0%
     *  vbus_voltage == dc_bus_overvoltage_ramp_end  =>  brake_duty_cycle += 100%
     *
     * Remarks:
     *  - This feature is active even when all motors are disarmed.
     *  - This feature is disabled if `brake_resistance` is non-positive.
     */
    bool enable_dc_bus_overvoltage_ramp;
    float dc_bus_overvoltage_ramp_start; //!< See `enable_dc_bus_overvoltage_ramp`.
                                                                      //!< Do not set this lower than your usual vbus_voltage,
                                                                      //!< unless you like fried brake resistors.
    float dc_bus_overvoltage_ramp_end; //!< See `enable_dc_bus_overvoltage_ramp`.
                                                                    //!< Must be larger than `dc_bus_overvoltage_ramp_start`,
                                                                    //!< otherwise the ramp feature is disabled.

}board_params_t;

typedef struct {
    float kp;
    float ki;
    float kd;
}pid_params_t;

typedef struct {
    s16 max_ebrk_torque;
    s16 dq_loop_bandwith;
    bool b_allow_pid_neg_torque; //if allow velocity pid or velocity limit pid output neg torque, to get the better dynamic performance
    bool  	b_asc_enable;
    s16   	asc_min_vel; //低于这个转速，可以启动Free Wheel 模式
    /**
     * if enabled，母线不能出现哪怕1A的反冲电流，所以有转速的情况下，并且转速大于regen_forbinden_min_vel，
     * 需要给已给小的正扭矩regen_forbinden_min_torque，使得不会有负扭矩产生，进而导致反充电流
     * regen_forbinden_min_torque的取值主要取决于iq的值，adc的分辨率，有效位数，相电流噪声，PI的性能。
    */
    bool	b_regen_forbidden;
    s16     regen_forbinden_min_torque;
    s16     regen_forbinden_min_vel;
    pid_params_t pid[PID_Max_ID];
}contrl_params_t;

typedef struct {
    s16 start_pp;
    s16 end_pp;
    s16 value;
}limitation_t;

typedef struct {
    float low;
    float high;
    float min_step;
    float normal_step;
}cross_zero_t;

typedef struct {
    motor_params_t m;
    board_params_t b;
    contrl_params_t c;
    limitation_t motor_protect[CONFIG_TEMP_PROT_NUM];
    limitation_t mosfet_protect[CONFIG_TEMP_PROT_NUM];
    s16 vbus_under_voltage;
    cross_zero_t cz;
}mc_config_t;

#define CONFIG_MAX_GEARS 4
#define CONFIG_GEAR_SPEED_TRQ_NUM 5

typedef struct {
    bool auto_hold;
    bool brk_shut_power;
    bool tcs_enable;
    s16   tcs_rpm_diff;
    float tcs_ki;
    s16   regen_torque;
    u16   regen_time;
    float noload_motor_J;//空转状态下的等效惯量
}settings_t;

typedef struct {
    s16 max_speed; //最大速度, rpm
    s16 max_torque;   //最大扭矩
    s16 max_dc_curr;   //最大母线电流
    u16 zero_accl; //零速启动扭矩给定时间，防止翘头
    u16 accl_time; //加速的扭矩斜坡时间
    u8  torque[CONFIG_GEAR_SPEED_TRQ_NUM]; //1000, 2000, 3000... RPM 最大给定扭矩的百分比（最大扭矩的百分比）
}gear_t;

typedef struct {
    settings_t 	 s;
    gear_t 		 gears[CONFIG_MAX_GEARS];
    s16 		 max_epm_rpm;
    s16			 max_epm_torque;
    s16 	     max_epm_back_rpm;
    s16 		 max_epm_back_torque;
    s16 		 max_autohold_torque;
    bool		 b_thro1_enable;
    bool		 b_thro1_invert;
    bool		 b_thro2_enable;
    bool		 b_thro2_invert;
    float 		 thro_start_vol;
    float 		 thro_end_vol;
    float 		 thro_min_vol;
    float 		 thro_max_vol;
    float        thro_start_torque; //转把启动输出的最小扭矩
    u16   		 thro_dec_time;
    bool         b_loading_det_enable;
    s16          loading_det_torque;
    s16          loading_det_vel;
    u16          loading_det_time;
}app_config_t;

#endif // MC_CONFIG_H
